package vn.edu.saigontech.ncms.web.offering;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.system.Shift;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.ExcelUtil;
import vn.edu.saigontech.ncms.web.BaseAction;
import vn.edu.saigontech.ncms.web.offering.report.ClassroomScheduleReport;
import vn.edu.saigontech.ncms.web.offering.report.DayScheduleDetail;
import vn.edu.saigontech.ncms.web.offering.report.OfferingScheduleDetail;
import vn.edu.saigontech.ncms.web.offering.report.RoomScheduleDetail;
import vn.edu.saigontech.ncms.web.offering.report.ShiftMap;
import vn.edu.saigontech.ncms.web.offering.report.ShiftScheduleDetail;
import vn.edu.saigontech.ncms.web.offering.report.StudyingScheduleReport;
import vn.edu.saigontech.ncms.web.offering.report.VideoScheduleReport;

@SuppressWarnings("serial")
public class ScheduleAction extends BaseAction {
	
	private String fromDate;
	private String toDate;
	
	private Date fromDateDate;
	private Date toDateDate;
	
	public String showSearch() {
		REQUEST().setAttribute("fromDate", DateUtil.getDateOfThisWeek(Calendar.MONDAY));
		REQUEST().setAttribute("toDate", DateUtil.getDateOfThisWeek(Calendar.SATURDAY));
		return SUCCESS;
	}
	
	public String search() {
		this.fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		this.toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date")); 
		REQUEST().setAttribute("offerings", OfferingService.INSTANCE.findByDate(fromDateDate, toDateDate));
		REQUEST().setAttribute("days", DateUtil.sequence(fromDateDate, toDateDate));
		
		this.addLoggingDetail(BaseAction.SEARCH, "logging.schedule.search");
		return SUCCESS;
	}
	
	public String showExport() {
		return SUCCESS;
	}
	
	public String export() {
		this.fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		this.toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date")); 
		
		Collection<Offering> offerings = OfferingService.INSTANCE.findByMonthAndYear(DateUtil.getMonth(fromDateDate), DateUtil.getYear(fromDateDate));
		REQUEST().setAttribute("offerings", offerings);
		REQUEST().setAttribute("fromDate", fromDate);
		REQUEST().setAttribute("toDate", toDate);
		REQUEST().setAttribute("dates", DateUtil.sequence(fromDateDate, toDateDate));
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.schedule.report", this.fromDate, this.toDate);
		return SUCCESS;
	}
	
	public String showSearchStudying() {
		this.fromDate = DateUtil.toString(DateUtil.startOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		this.toDate = DateUtil.toString(DateUtil.endOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		return SUCCESS;
	}
	
	public String reportStudying() {
		StudyingScheduleReport report = new StudyingScheduleReport(DateUtil.fromString(this.fromDate, this.i18n("format.date")), 
				DateUtil.fromString(this.toDate, this.i18n("format.date")));
		RESPONSE().setContentType("application/vnd.ms-excel");
		RESPONSE().setHeader("Content-disposition","attachment; filename=\""+ "StudyingSchedule-" + this.fromDate + "-" + this.toDate + ".xls\"");
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("Report");
			
			HSSFFont fontBold18 = ExcelUtil.makeFont(workbook, "Times New Roman", 18, true);
			HSSFFont fontBold12 = ExcelUtil.makeFont(workbook, "Times New Roman", 12, true);
			HSSFFont fontBold10 = ExcelUtil.makeFont(workbook, "Times New Roman", 10, true);
			HSSFFont fontNormal11 = ExcelUtil.makeFont(workbook, "Times New Roman", 11, false);
			HSSFFont fontNormal10 = ExcelUtil.makeFont(workbook, "Times New Roman", 10, false);
			
			HSSFCellStyle styleCenter_B18 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold18);
			HSSFCellStyle styleCenter_B12 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold12);
			HSSFCellStyle styleCenter_B10 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold10);
			HSSFCellStyle styleCenter_N11 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal11);
			HSSFCellStyle styleCenter_N10 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal10);
			HSSFCellStyle styleLeft_N10 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER, fontNormal10);
			
			styleCenter_B18.setFillBackgroundColor((short)1);
			HSSFRow row = sheet.createRow(0);
			HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B18);
			String name = "EVENING STUDYING SCHEDULE";
			cell.setCellValue(new HSSFRichTextString(name));
			sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 9));
//			sheet.addMergedRegion(new Region(0, (short)0, 1, (short)9));
			
			row = sheet.createRow(2);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 0, "Day", 256*7, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 1, "Date", 256*9, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 2, "Time", 256*15, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 3, "Class", 256*10, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 4, "Open day", 256*12, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 5, "Room", 256*9, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 6, "Number of Student", 256*12, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 7, "Instructor", 256*30, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 8, "Video", 256*10, styleCenter_B12);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 9, "Quiz and Sample test", 256*10, styleCenter_B10);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 10, "Note", 256*20, styleCenter_B12);
			
			int r = 3;
			int size = report.getRange().size();
			List<DayScheduleDetail> daySchedules = report.getDaySchedules();
			for(int i = 0; i < size; i++) {
				row = sheet.createRow(r);
				row.setHeightInPoints(20);
				DayScheduleDetail daySchedule = daySchedules.get(i);
				
				int mergeRows = 1;
				List<OfferingScheduleDetail> offeringSchedules = daySchedule.getOfferingSchedules();
				if(offeringSchedules.size() != 0) {
					mergeRows = offeringSchedules.size();
				}
				
				// Day
				cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString(daySchedule.getDay().toUpperCase()));
				cell.setCellStyle(styleCenter_B10);
				sheet.addMergedRegion(new CellRangeAddress(r, r + mergeRows - 1, 0, 0));
//				sheet.addMergedRegion(new Region(r, (short)0, r + mergeRows - 1, (short)0));
				
				// Date
				cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString(daySchedule.getDate()));
				cell.setCellStyle(styleCenter_N11);
				sheet.addMergedRegion(new CellRangeAddress(r, r + mergeRows - 1, 1, 1));
//				sheet.addMergedRegion(new Region(r, (short)1, r + mergeRows - 1, (short)1));
				
				if(offeringSchedules.size() > 0) {
					for(int j = 0; j < mergeRows; j++, r++) {
						OfferingScheduleDetail offeringSchedule = offeringSchedules.get(j);
						if(j != 0) {
							row = sheet.createRow(r);
							row.setHeightInPoints(20);
						}
						
						cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getShift()));
						
						cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_B10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getOfferingName()));
						
						cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getOpenDay()));
						
						cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getRoom()));
						
						cell = row.createCell(6, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getNumOfStudent()));
						
						cell = row.createCell(7, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleLeft_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getInstructor()));
						
						cell = row.createCell(8, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getVideo()));
						
						cell = row.createCell(9, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getTest()));
						
						cell = row.createCell(10, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getNote()));
					}
				} else 
					r++;
			}
			workbook.write(RESPONSE().getOutputStream());
		} catch (Exception e) {
			// TODO
		}
		this.addLoggingDetail(BaseAction.REPORT, "logging.studying.report", this.fromDate, this.toDate);
		return NONE;
	}
	
	public String showSearchVideo() {
		this.fromDate = DateUtil.toString(DateUtil.startOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		this.toDate = DateUtil.toString(DateUtil.endOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		return SUCCESS;
	}
	
	public String reportVideo() {
		VideoScheduleReport report = new VideoScheduleReport(DateUtil.fromString(this.fromDate, this.i18n("format.date")), 
				DateUtil.fromString(this.toDate, this.i18n("format.date")));
		RESPONSE().setContentType("application/vnd.ms-excel");
		RESPONSE().setHeader("Content-disposition","attachment; filename=\""+ "Video Schedule-" + this.fromDate + "-" + this.toDate + ".xls\"");
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("Report");
			
			HSSFFont fontBold18 = ExcelUtil.makeFont(workbook, "Times New Roman", 18, true);
			HSSFFont fontNormal20 = ExcelUtil.makeFont(workbook, "Times New Roman", 20, false);
			HSSFFont fontBold13 = ExcelUtil.makeFont(workbook, "Times New Roman", 13, true);
			HSSFFont fontBold12 = ExcelUtil.makeFont(workbook, "Times New Roman", 12, true);
			HSSFFont fontNormal10 = ExcelUtil.makeFont(workbook, "Times New Roman", 10, false);
			
			HSSFCellStyle styleCenter_B18 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold18);
			HSSFCellStyle styleCenter_N20 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal20);
			HSSFCellStyle styleCenter_B13 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold13);
			HSSFCellStyle styleCenter_B12 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold12);
			HSSFCellStyle styleCenter_N10 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal10);
			HSSFCellStyle styleLeft_N10 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER, fontNormal10);
			
			HSSFCellStyle styleCenter_B12_Fill = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold12);
			styleCenter_B12_Fill.setFillPattern(HSSFCellStyle.FINE_DOTS);
			styleCenter_B12_Fill.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
			
			HSSFFont fontNormal12 = ExcelUtil.makeFont(workbook, "Times New Roman", 12, false);
			HSSFCellStyle styleCenter_N12_BG = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal12);
			styleCenter_N12_BG.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			styleCenter_N12_BG.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
			
			ExcelUtil.makeBorder(styleCenter_B18);
			ExcelUtil.makeBorder(styleCenter_N20);
			ExcelUtil.makeBorder(styleCenter_B13);
			ExcelUtil.makeBorder(styleCenter_B12);
			ExcelUtil.makeBorder(styleCenter_N10);
			ExcelUtil.makeBorder(styleLeft_N10);
			ExcelUtil.makeBorder(styleCenter_B12_Fill);
			ExcelUtil.makeBorder(styleCenter_N12_BG);
			
			HSSFRow row = sheet.createRow(0);
			row.setHeightInPoints(70);
			HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B18);
			cell.setCellValue(new HSSFRichTextString("EVENING STUDYING PROGRAM" + "\n" + "Video Schedule" + "\n" + report.getDate()));
			
			ExcelUtil.createBlankCells(1, 10, row, styleCenter_B18);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10));
			
			row = sheet.createRow(1);
			row.setHeightInPoints(25);
			
			cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_N20);
			cell.setCellValue(new HSSFRichTextString("Scheduled equipment"));
			
			ExcelUtil.createBlankCells(1, 6, row, styleCenter_N20);
			sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
			
			cell = row.createCell(7, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B12_Fill);
			cell.setCellValue(new HSSFRichTextString(""));
			
			cell = row.createCell(8, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_N20);
			cell.setCellValue(new HSSFRichTextString("Ordered equipment"));
			
			ExcelUtil.createBlankCells(9, 10, row, styleCenter_N20);
			sheet.addMergedRegion(new CellRangeAddress(1, 1, 8, 10));
//			sheet.addMergedRegion(new Region(1, (short)8, 1, (short)10));
			
			row = sheet.createRow(2);
			row.setHeightInPoints(20);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 0, "Day", 256*7, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 1, "Date", 256*9, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 2, "Time", 256*15, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 3, "Class", 256*10, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 4, "Open day", 256*11, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 5, "Room", 256*9, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 6, "Instructor", 256*30, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 7, "", 256*5, styleCenter_B12_Fill);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 8, "Time", 256*15, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 9, "Room", 256*9, styleCenter_B13);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 10, "Instructor", 256*30, styleCenter_B13);
			
			int r = 3;
			int size = report.getRange().size();
			List<DayScheduleDetail> daySchedules = report.getDaySchedules();
			for(int i = 0; i < size; i++) {
				row = sheet.createRow(r);
				row.setHeightInPoints(30);
				DayScheduleDetail daySchedule = daySchedules.get(i);
				
				int mergeRows = 1;
				List<OfferingScheduleDetail> offeringSchedules = daySchedule.getOfferingSchedules();
				if(offeringSchedules.size() != 0) {
					mergeRows = offeringSchedules.size();
				}
				
				// Day
				cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString(daySchedule.getDay().toUpperCase()));
				cell.setCellStyle(styleCenter_B12);
				sheet.addMergedRegion(new CellRangeAddress(r, r + mergeRows - 1, 0, 0));
//				sheet.addMergedRegion(new Region(r, (short)0, r + mergeRows - 1, (short)0));
				
				// Date
				cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString(daySchedule.getDate()));
				cell.setCellStyle(styleCenter_B12);
				sheet.addMergedRegion(new CellRangeAddress(r, r + mergeRows - 1, 1, 1));
//				sheet.addMergedRegion(new Region(r, (short)1, r + mergeRows - 1, (short)1));
				
				if(offeringSchedules.size() > 0) {
					for(int j = 0; j < mergeRows; j++, r++) {
						OfferingScheduleDetail offeringSchedule = offeringSchedules.get(j);
						if(j != 0) {
							row = sheet.createRow(r);
							row.setHeightInPoints(30);
							
							ExcelUtil.createBlankCells(0, 1, row, styleCenter_B12);
						}
						cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_B12);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getShift()));
						
						cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_B12);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getOfferingName()));
						
						cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getOpenDay()));
						
						cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getRoom()));
						
						cell = row.createCell(6, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleLeft_N10);
						cell.setCellValue(new HSSFRichTextString(offeringSchedule.getInstructor()));
						
						cell = row.createCell(7, HSSFCell.CELL_TYPE_STRING);
						cell.setCellStyle(styleCenter_B12_Fill);
						cell.setCellValue(new HSSFRichTextString(""));
						
						ExcelUtil.createBlankCells(8, 10, row, styleCenter_N10);
					}
				} else {
					
					cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_N12_BG);
					cell.setCellValue(new HSSFRichTextString(""));
					
					cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_N12_BG);
					cell.setCellValue(new HSSFRichTextString(""));
					
					cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_N12_BG);
					cell.setCellValue(new HSSFRichTextString(""));
					
					cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_N12_BG);
					cell.setCellValue(new HSSFRichTextString(""));
					
					cell = row.createCell(6, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_N12_BG);
					cell.setCellValue(new HSSFRichTextString(""));
					
					cell = row.createCell(7, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_B12_Fill);
					cell.setCellValue(new HSSFRichTextString(""));
					
					ExcelUtil.createBlankCells(8, 10, row, styleCenter_N10);
					r++;
				}
			}
			sheet.addMergedRegion(new CellRangeAddress(1, r - 1, 7, 7));
			
			workbook.write(RESPONSE().getOutputStream());
		} catch (Exception e) {
			// TODO
		}
		this.addLoggingDetail(BaseAction.REPORT, "logging.studying.report", this.fromDate, this.toDate);
		return NONE;
	}
	
	public String showSearchClassroom() {
		this.fromDate = DateUtil.toString(DateUtil.getDateOfThisWeek(Calendar.MONDAY));
		this.toDate = DateUtil.toString(DateUtil.getDateOfThisWeek(Calendar.SATURDAY));
		return SUCCESS;
	}
	
	public String reportClassroom() {
		Date fromDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		ClassroomScheduleReport report = new ClassroomScheduleReport(fromDate, toDate);
		RESPONSE().setContentType("application/vnd.ms-excel");
		RESPONSE().setHeader("Content-disposition","attachment; filename=\""+ "StudyingSchedule-" + this.fromDate + "-" + this.toDate + ".xls\"");
		
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("Report");
			
			HSSFFont fontBold13 = ExcelUtil.makeFont(workbook, "Times New Roman", 13, true);
			HSSFFont fontBold18 = ExcelUtil.makeFont(workbook, "Times New Roman", 18, true);
			HSSFFont fontNormal13 = ExcelUtil.makeFont(workbook, "Times New Roman", 13, false);
			
			HSSFCellStyle styleCenter_B13 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold13);
			HSSFCellStyle styleCenter_B18 = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold18);
			
			HSSFCellStyle styleCenter_N13_BG = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal13);
			styleCenter_N13_BG.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			styleCenter_N13_BG.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
			
			HSSFCellStyle style_BG_High = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontNormal13);
			style_BG_High.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			style_BG_High.setFillForegroundColor(new HSSFColor.GREY_50_PERCENT().getIndex());
			
			HSSFCellStyle styleCenter_B13_BG = ExcelUtil.makeCellStyle(workbook, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, fontBold13);
			styleCenter_B13_BG.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			styleCenter_B13_BG.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
			
			ExcelUtil.makeBorder(styleCenter_B13);
			ExcelUtil.makeBorder(styleCenter_N13_BG);
			ExcelUtil.makeBorder(styleCenter_B13_BG);
			
			HSSFRow row = sheet.createRow(0);
			row.setHeightInPoints(60);
			HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B18);
			cell.setCellValue(new HSSFRichTextString("CLASSROOM SCHEDULE" + "\n" + report.getDate()));

			List<Date> range = report.getRange();
			int rangeSize = range.size();
			int sheetSize = rangeSize + 1;
			ExcelUtil.createBlankCells(1, rangeSize, row, styleCenter_B18);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, sheetSize));
			
			int r = 1;			
			row = sheet.createRow(r);
			row.setHeightInPoints(20);
			
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 0, "Room" + "\n" + "(Anna)", 256*14, styleCenter_B13_BG);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 1, "Time", 256*20, styleCenter_B13_BG);
			
			for(int i = 0; i < rangeSize; i++) {
				String dayValue = DateUtil.getDateOfWeekAsString(range.get(i));
				ExcelUtil.makeColumnLabel(workbook, sheet, row, i + 2, dayValue, 256*18, styleCenter_B13_BG);
			}
			r = 2;
			row = sheet.createRow(r);
			row.setHeightInPoints(20);
			
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 0, "", 256*14, styleCenter_B13_BG);
			ExcelUtil.makeColumnLabel(workbook, sheet, row, 1, "", 256*20, styleCenter_B13_BG);
			
			for(int i = 0; i < rangeSize; i++) {
				Date date = range.get(i);
				String dateValue = DateUtil.getDayOfMonth(date) + "-" + DateUtil.getMonthAsString(date);
				ExcelUtil.makeColumnLabel(workbook, sheet, row, i + 2, dateValue, 256*18, styleCenter_B13_BG);
			}
			
			sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
			sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1));
			
			List<RoomScheduleDetail> roomSchedules = report.getRoomSchedules();
			r = 3;
			for(int i = 0; i < roomSchedules.size(); i++) {
				row = sheet.createRow(r);
				row.setHeightInPoints(28);
				
				RoomScheduleDetail roomSchedule = roomSchedules.get(i);
				
				List<ShiftScheduleDetail> shiftSchedules = roomSchedule.getShiftSchedules();
				int mergeRows = shiftSchedules.size();
				
				// Room
				cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString("Room " + roomSchedule.getRoom().getName()));
				cell.setCellStyle(styleCenter_B13_BG);
				sheet.addMergedRegion(new CellRangeAddress(r, r + mergeRows - 1, 0, 0));

				for(int j = 0; j < mergeRows; j++, r++) {
					ShiftScheduleDetail shiftSchedule = shiftSchedules.get(j);
					if(j != 0) {
						row = sheet.createRow(r);
						row.setHeightInPoints(28);
					}
					
					cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
					cell.setCellStyle(styleCenter_B13);
					cell.setCellValue(new HSSFRichTextString(shiftSchedule.getShift().toString()));
					
					Map<Date, Boolean > details = shiftSchedule.getDetails();
					for(int k = 0; k < range.size(); k++) {
						Date key = range.get(k);
						Boolean value = details.get(key);
						if(value) {
							cell = row.createCell(k + 2, HSSFCell.CELL_TYPE_STRING);
							cell.setCellStyle(styleCenter_B13);
							cell.setCellValue(new HSSFRichTextString("√"));
						} else {
							cell = row.createCell(k + 2, HSSFCell.CELL_TYPE_STRING);
							cell.setCellStyle(styleCenter_N13_BG);
							cell.setCellValue(new HSSFRichTextString("No classes"));
						}
					}
				}
			}
			// TONG KET
			row = sheet.createRow(r);
			row.setHeightInPoints(18);
			
			cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B13);
			cell.setCellValue(new HSSFRichTextString("Tổng kết phòng học"));
			
			Map<Date, ShiftMap> scheduleMap = report.getScheduleMap();
			
			cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B13);
			cell.setCellValue(new HSSFRichTextString("Ca 1:"));
			
			for(int i = 0; i < rangeSize; i++) {
				Date date = range.get(i);
				cell = row.createCell(i + 2, HSSFCell.CELL_TYPE_STRING);
				cell.setCellStyle(styleCenter_B13);
				cell.setCellValue(new HSSFRichTextString(scheduleMap.get(date).get(Shift.SHIFT_1) + ""));
			}
			
			r = r + 1;
			row = sheet.createRow(r);
			row.setHeightInPoints(18);
			
			cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B13);
			
			cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(styleCenter_B13);
			cell.setCellValue(new HSSFRichTextString("Ca 2:"));
			
			for(int i = 0; i < rangeSize; i++) {
				Date date = range.get(i);
				cell = row.createCell(i + 2, HSSFCell.CELL_TYPE_STRING);
				cell.setCellStyle(styleCenter_B13);
				cell.setCellValue(new HSSFRichTextString(scheduleMap.get(date).get(Shift.SHIFT_2) + ""));
			}
			
			sheet.addMergedRegion(new CellRangeAddress(r - 1, r, 0, 0));
			
			workbook.write(RESPONSE().getOutputStream());
		} catch (Exception e) {
			// TODO
		}
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.classroom.report", this.fromDate, this.toDate);
		return NONE;
	}
	
	public boolean isValid(Date date, Date startDate, Date endDate) {
		if(date == null) return false;
		return DateUtil.isBetween(date, startDate, endDate);
	}
	
	public boolean isBetween(Date date) {
		if(date == null) return false;
		return DateUtil.isBetween(date, this.fromDateDate, this.toDateDate);
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public Date getFromDateDate() {
		return fromDateDate;
	}

	public void setFromDateDate(Date fromDateDate) {
		this.fromDateDate = fromDateDate;
	}

	public Date getToDateDate() {
		return toDateDate;
	}

	public void setToDateDate(Date toDateDate) {
		this.toDateDate = toDateDate;
	}
}
